package com.rla.controller;

import java.util.List;
import java.util.Arrays;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.rla.common.annotation.Anonymous;
import com.rla.domain.IntegralTask;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.rla.common.annotation.RepeatSubmit;
import com.rla.common.annotation.Log;
import com.rla.common.core.controller.BaseController;
import com.rla.common.core.domain.PageQuery;
import com.rla.common.core.domain.R;
import com.rla.common.utils.validate.AddGroup;
import com.rla.common.utils.validate.EditGroup;
import com.rla.common.enums.BusinessType;
import com.rla.common.utils.poi.ExcelUtil;
import com.rla.domain.vo.MallOrderVo;
import com.rla.domain.bo.MallOrderBo;
import com.rla.service.IMallOrderService;
import com.rla.common.core.page.TableDataInfo;

/**
 * 零售订单
 *
 * @author rla
 * @date 2025-08-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/order")
public class MallOrderController extends BaseController {

    private final IMallOrderService iMallOrderService;

    /**
     * 查询零售订单列表
     */
    @GetMapping("/list")
    public TableDataInfo<MallOrderVo> list(MallOrderBo bo, PageQuery pageQuery) {
        return iMallOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出零售订单列表
     */
    @Log(title = "零售订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MallOrderBo bo, HttpServletResponse response) {
        List<MallOrderVo> list = iMallOrderService.queryList(bo);
        ExcelUtil.exportExcel(list, "零售订单", MallOrderVo.class, response);
    }

    /**
     * 获取零售订单详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<MallOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iMallOrderService.queryById(id));
    }

    /**
     * 新增零售订单
     */
    @Log(title = "零售订单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<String> add(@Validated(AddGroup.class) @RequestBody MallOrderBo bo) {

        boolean r = iMallOrderService.insertByBo(bo);
        if (r) {
            return R.okStrdata(bo.getId());
        } else {
            return R.fail();
        }
    }

    /**
     * 修改零售订单
     */
    @Log(title = "零售订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MallOrderBo bo) {
        return toAjax(iMallOrderService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除零售订单
     *
     * @param ids 主键串
     */
    @Log(title = "零售订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iMallOrderService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }


    /**
     * 确认收货
     */
    @Log(title = "确认收货", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/confirmReceipt")
    public R<Void> confirmReceipt(@Validated(EditGroup.class) @RequestBody MallOrderBo bo) {
        return toAjax(iMallOrderService.confirmReceipt(bo) ? 1 : 0);
    }

    /**
     * 获取订单物流信息
     */
    @Log(title = "获取订单物流信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/getLogisticsInfo")
    public R<JSONObject> getLogisticsInfo(@Validated(AddGroup.class) @RequestBody MallOrderBo bo) {

        return R.ok(iMallOrderService.getLogisticsInfo(bo));
    }

    /**
     * 退款申请
     */
    @Log(title = "退款申请", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/refundApply")
    public R<Void> refundApply(@Validated(EditGroup.class) @RequestBody MallOrderBo bo) {
        return toAjax(iMallOrderService.refundApply(bo) ? 1 : 0);
    }


    /**
     * 取消退款申请
     */
    @Log(title = "取消退款申请", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/cancelRefund")
    public R<Void> cancelRefund(@Validated(EditGroup.class) @RequestBody MallOrderBo bo) {
        return toAjax(iMallOrderService.cancelRefund(bo) ? 1 : 0);
    }



    /*
    * 获取订单角标
    * */
    @PostMapping("/queryOrderNum")
    public R<Map<String,Object>> queryOrderNum() {
        return R.ok(iMallOrderService.queryOrderNum());
    }


    /*
     * 确认发货中台推送
     * */
    @Anonymous
    @PostMapping("/deliverByPd")
    public R<Void> deliverByPd(@Validated(EditGroup.class) @RequestBody MallOrderBo bo) {
        return toAjax(iMallOrderService.deliverByPd(bo) ? 1 : 0);
    }


    /**
     * 订单赠送积分
     */
    @GetMapping("/rewardPoints")
    public R<Void> rewardPoints(@RequestParam("userId") String userId, @RequestParam("amount") Double amount,@RequestParam("orderId") String orderId){
        return toAjax(iMallOrderService.rewardPoints(userId,amount,orderId));
    }

    /**
     * 退单扣减积分
     */
    @Anonymous
    @PostMapping("/deductPoints")
    public R<Void> deductPoints(@RequestBody Map<String,String> map){
        return toAjax(iMallOrderService.deductPoints(map.get("orderId")));
    }

}
